package com.xframe.domain.repository;

import com.xframe.domain.role.IUserCredentials;
import com.xframe.domain.sys.SysDataSource;
import com.xframe.xdal.core.annotation.XAction;
import com.xframe.xdal.core.annotation.XExecTime;
import com.xframe.xdal.core.constant.ErrorCode;
import com.xframe.xdal.core.constant.ErrorMsg;
import com.xframe.xdal.core.exception.DalFrameException;
import com.xframe.xdal.core.model.ConditionValue;
import com.xframe.xdal.core.model.FindOptions;
import com.xframe.xdal.core.model.Operator;
import com.xframe.xdal.core.model.mapping.ITableInfo;
import com.xframe.xdal.core.model.mapping.TableInfoBuilder;
import com.xframe.xdal.core.tran.TransactionIsolation;
import com.xframe.xdal.core.tran.XTransaction;
import com.xframe.xdal.core.util.CollectionUtil;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据源模型存储库
 *
 * @author xufeng
 * @version 1.0
 * @date 2023-04-06
 */
@XExecTime
@Log4j2
public class SysDataSourceRepository extends SysBaseRepository{

    /**
     * 测试连接数据源
     */
    @XAction(code = "dataSourecTestConnect",desc = "连接测试数据源")
    public Object dataSourecTestConnect(SysDataSource sysDataSource){
        log.info("call testDataSourecConnect");
        try {
            return sysDataSource.testDataSourecConnect();
        }catch (java.lang.IllegalArgumentException exception){
            throw new DalFrameException(ErrorCode.DATABASE_TYPE_ERROR, ErrorMsg.DATABASE_TYPE_ERROR_MSG);
        }catch (Exception exception){
            throw new DalFrameException(ErrorCode.DATABASE_CONNECT_ERROR, ErrorMsg.DATABASE_CONNECT_ERROR_MSG);
        }
    }


    @XAction(code = "dataSourecTestConnectByBatch",desc = "连接测试数据源")
    public Object batchTestConnect(List<String> ids){
        log.info("call batchTestConnect");
        try {
            // 组装查询条件 获得 sourceId = ? or sourceId = ? or sourceId = ? 的where 语句
            FindOptions findOptions = FindOptions.defFindAllOptions();
            for(String id:ids){
                findOptions.addConditionValue(new ConditionValue("sourceId", Operator.EQ,id,"X"));
            }
            // 获得对象集合
            List<SysDataSource> sysDataSources = this.defDataBaseAccess.findAll(SysDataSource.class,findOptions);

            // 容错，改集合不能为空
            if(CollectionUtil.isEmpty(sysDataSources)){
                throw new DalFrameException(ErrorCode.COLLECTION_ISEMPTY, ErrorMsg.COLLECTION_ISEMPTY_MSG);
            }
            // 并发执行
            sysDataSources.parallelStream().forEach( sysDataSource ->{
                sysDataSource.testDataSourecConnect();
            });


            return "ALL OK";
        }catch (java.lang.IllegalArgumentException exception){
            throw new DalFrameException(ErrorCode.DATABASE_TYPE_ERROR, ErrorMsg.DATABASE_TYPE_ERROR_MSG);
        }catch (Exception exception){
            throw new DalFrameException(ErrorCode.DATABASE_CONNECT_ERROR, ErrorMsg.DATABASE_CONNECT_ERROR_MSG);
        }
    }

    
    @XTransaction(tranLevel = TransactionIsolation.TRANSACTION_READ_UNCOMMITTED)
    @XAction(code = "testTran",desc = "测试事务")
    public String testTran(IUserCredentials userCredentials){
        ITableInfo tableInfo = TableInfoBuilder.BuildTableInfoByAnnotation(SysDataSource.class);
        SysDataSource sysDataSource = new SysDataSource();
        sysDataSource.setCode("test0912329");
        sysDataSource.setDesc("测试数据源描述");
        sysDataSource.setName("测试数据源");
        sysDataSource.setHost("47.100.42.45");
        sysDataSource.setPwd("woaini5566...");
        sysDataSource.setPort(3306);
        sysDataSource.setDbName("dataexchange");
        sysDataSource.setType("MySql");
        sysDataSource.setUserName("root");
        this.setBaseAttrVal(sysDataSource,userCredentials);

        List<SysDataSource> list = new ArrayList<>();
        this.createOrUpdate(sysDataSource,tableInfo);

//        this.save(sysDataSource,tableInfo);
//        //this.deleteById(1689471394422853632L, tableInfo);
//        List<SysDataSource> list = this.queryModle(SysDataSource.class,tableInfo, FindOptions.defFindAllOptions());
//        log.info(list.size());
//        SysDataSource sysDataSource1 = this.findbyAk(SysDataSource.class,"0810code");
//        sysDataSource1.setBaseAttrVal(userCredentials);
//        sysDataSource1.setDesc("测试数据源描述11");
//        this.update(sysDataSource1,tableInfo);
        return "OK";
    }
}
